package com.example.service;

import com.example.dao.NxQuestionInfoDao;
import com.example.dao.NxQuestionTypeInfoDao;
import com.example.entity.NxQuestionInfo;
import com.example.vo.NxQuestionInfoVo;
import com.example.vo.NxQuestionTypeInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class NxQuestionInfoService {

    @Resource
    private NxQuestionInfoDao nxQuestionInfoDao;

    @Resource
    private NxQuestionTypeInfoDao nxQuestionTypeInfoDao;

    public NxQuestionInfo add(NxQuestionInfo nxQuestionInfo) {
        List<NxQuestionTypeInfoVo> questionType = nxQuestionTypeInfoDao.findByName(nxQuestionInfo.getQuestionType());
        nxQuestionInfo.setScore(questionType.get(0).getScore());
        nxQuestionInfoDao.insertSelective(nxQuestionInfo);
        return nxQuestionInfo;
    }

    public void delete(Long id) {
        nxQuestionInfoDao.deleteByPrimaryKey(id);
    }

    public void update(NxQuestionInfo nxQuestionInfo) {
        nxQuestionInfoDao.updateByPrimaryKeySelective(nxQuestionInfo);
    }

    public NxQuestionInfo findById(Long id) {
        return nxQuestionInfoDao.selectByPrimaryKey(id);
    }

    public List<NxQuestionInfoVo> findAll() {
        return nxQuestionInfoDao.findByName("all");
    }

    public PageInfo<NxQuestionInfoVo> findPage(String name, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
        List<NxQuestionInfoVo> all = findAllPage(request, name);
        return PageInfo.of(all);
    }

    public List<NxQuestionInfoVo> findAllPage(HttpServletRequest request, String name) {
		return nxQuestionInfoDao.findByName(name);
    }

}
